package com.security.oauthfilter;

import com.security.controller.OauthController;
import com.security.exception.OauthTokenNotFoundException;
import com.security.handler.Oauth2ParamsHandler;
import com.security.validator.Oauth2ParamsValidator;
import com.security.vo.OauthBaseVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by 陈圣融 on 2017-07-29.
 */
public class Oauth2AccessDeniedExceptionFilter implements Filter {
    Logger logger = LoggerFactory.getLogger(Oauth2AccessDeniedExceptionFilter.class);

    private Oauth2ParamsHandler oauth2ParamsHandler = new Oauth2ParamsHandler();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        OauthBaseVo oauthBaseErrorVo = null;
        try {
            if (!oauth2ParamsHandler.isNoCheckUrl(request)) {
                String accessToken = request.getParameter("access_token");
                if (StringUtils.isEmpty(accessToken)) {
                    throw new OauthTokenNotFoundException("token not found");
                }
            }
            filterChain.doFilter(servletRequest, servletResponse);
        } catch (IOException ex) {
            throw ex;
        } catch (Exception exception) {
            oauthBaseErrorVo = new OauthBaseVo();
            if (exception instanceof OauthTokenNotFoundException) {
                oauthBaseErrorVo.setResultCode(Oauth2ParamsValidator.ERROR_CODE_TOKEN_ABSENT);
                oauthBaseErrorVo.setResutlMessage(Oauth2ParamsValidator.oauth2ErrorMap.get(Oauth2ParamsValidator.ERROR_CODE_TOKEN_ABSENT));
            } else if (exception instanceof AccessDeniedException) {
                oauthBaseErrorVo.setResultCode(Oauth2ParamsValidator.ERROR_CODE_AUTHENTICATION_ACCESSDENIED);
                oauthBaseErrorVo.setResutlMessage(Oauth2ParamsValidator.oauth2ErrorMap.get(Oauth2ParamsValidator.ERROR_CODE_AUTHENTICATION_ACCESSDENIED));
            }
            oauth2ParamsHandler.forwardUnoauth(request, response, oauthBaseErrorVo);
        }
    }

    public Oauth2ParamsHandler getOauth2ParamsHandler() {
        return oauth2ParamsHandler;
    }

    public void setOauth2ParamsHandler(Oauth2ParamsHandler oauth2ParamsHandler) {
        this.oauth2ParamsHandler = oauth2ParamsHandler;
    }

    @Override
    public void destroy() {

    }


}
